package routes

import (
	"todo/api"
	"todo/middleware"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

// 返回一个gin引擎
func NewRouter() *gin.Engine {
	// 创建gin实例
	r := gin.Default()
	// 写入cookie
	store := cookie.NewStore([]byte("mySecret"))
	// 用session存储
	r.Use(sessions.Sessions("mySession", store))
	// 路由
	// 基础路由
	v1 := r.Group("api/v1")
	{
		// 用户操作
		v1.POST("user/register", api.UserRegister)
		v1.POST("user/login", api.UserLogin)
		authed := v1.Group("/")
		//	分组进行中间器的认证
		authed.Use(middleware.JWT())
		{
			//创建任务的路由
			authed.POST("task", api.CreateTask)
			authed.GET("task/:id", api.ShowTask)
			authed.GET("tasks", api.ListTask)
			authed.PUT("task/:id", api.UpdateTask)
			authed.POST("search", api.SearchTask)
			authed.DELETE("task/:id", api.DeleteTask)
		}

	}
	return r
}
